#include <bits/stdc++.h>
using namespace std;

using llt = long long;
using arint = array<int, 27>;
using arll = array<llt, 27>;
using aall = array<arll, 27>;
using aaal = array<aall, 27>;

int N;
int Q;
string S;
int BlockSize;
int BlockCnt;

vector<int> Block;
vector<int> Left, Right;

vector<arint> Single;
vector<array<arint, 27>> Double;
vector<aaal> D;
vector<arint> Cnt;

void init(){
    BlockSize = sqrt(N) + 1;
    // BlockSize = 12;
    BlockCnt = N / BlockSize;
    if(N % BlockSize) ++BlockCnt;

    Block.assign(N + 1, 0);
    Cnt.assign(N + 1, {0});

	Left.assign(BlockCnt + 1, 0);
	Right.assign(BlockCnt + 1, 0);

	Single.assign(BlockCnt + 1, {0});
	Double.assign(BlockCnt + 1, array<arint, 27>({arint({0})}));

	D.assign(BlockCnt + 1, aaal({aall({arll({0LL})})}));

    int p = 1;
    for(int i=1;i<=BlockCnt;++i){
        fill(Block.begin() + (Left[i] = p), Block.begin() + (Right[i] = min(p + BlockSize - 1, N)) + 1, i);
        for(;p<=Right[i];++p){
            int c = S[p] - 'a' + 1;
            copy(Cnt[p - 1].begin(), Cnt[p - 1].end(), Cnt[p].begin());
            ++Cnt[p][c];

            for(int b=1;b<=26;++b){
                for(int a=1;a<=26;++a){
                    D[i][b][a][c] += Double[i][b][a];
                }
                Double[i][b][c] += Single[i][b];
            }
            ++Single[i][c];
        }
    }    

	for(int i=1;i<=BlockCnt;++i){
		for(int a=1;a<=26;++a){
			for(int b=1;b<=26;++b){
				for(int c=1;c<=26;++c){
					auto & tabci = D[i][a][b][c];
					tabci += D[i - 1][a][b][c];
                    tabci += D[i - 1][0][a][b] * Single[i][c];
					tabci += D[i - 1][0][0][a] * Double[i][b][c];
				}

				D[i][0][a][b] = D[i - 1][0][a][b] + Double[i][a][b];
				D[i][0][a][b] += D[i - 1][0][0][a] * Single[i][b];
			}

			D[i][0][0][a] = D[i - 1][0][0][a] + Single[i][a];
		}
	}

	// for(int i=1;i<=BlockCnt;++i){
	// 	cout << i << ": [" << 1 << ", " << Right[i] << "]\n"; 
	// 	for(int a=1;a<=26;++a){
	// 		if(D[i][0][0][a]){
	// 			cout << "(" << (char)(a+'a'-1) << ": " << D[i][0][0][a] << ")";
	// 		}
	// 		for(int b=1;b<=26;++b){
	// 			if(D[i][0][a][b]){
	// 			    cout << "(" << (char)(a+'a'-1) << (char)(b+'a'-1) << ": " << D[i][0][a][b] << ")";
	// 		    }
	// 			for(int c=1;c<=26;++c){
	// 				if(D[i][a][b][c]){
	// 					cout << "(" << (char)(a+'a'-1) << (char)(b+'a'-1) << (char)(c+'a'-1) << ": " << D[i][a][b][c] << ")";
	// 				}
	// 			}				
	// 		}			
	// 	}
	// 	cout << endl;
	// }	

    return;
}

int L, R;
char T[5];

void calcPre(int s, int e, int & pre_a, int & pre_ab, int & pre_abc){
    pre_a = pre_ab = pre_abc = 0;
    if(s > e) return;

    int c = T[2] - 'a' + 1;    
    pre_a = 0;
    int suf = Cnt[e][c] - Cnt[s - 1][c];
    if(S[s] == T[0]) pre_a = 1;
    if(S[s] == T[2]) --suf;

    for(int i=s+1;i<=e;++i){
        if(S[i] == T[2]){
            --suf;
        }
        if(S[i] == T[1]){
            pre_abc += pre_a * suf;
            pre_ab += pre_a;
        }
        if(S[i] == T[0]){
            ++pre_a;
        }
    }    

    return;
}

void calcSuf(int s, int e, int & suf_c, int & suf_bc, int & suf_abc){
    suf_c = suf_bc = suf_abc = 0;
    if(s > e) return;

    int a = T[0] - 'a' + 1;
    suf_c = 0;
    int pre = Cnt[e][a] - Cnt[s - 1][a];
    if(S[e] == T[2]) suf_c = 1;
    if(S[e] == T[0]) --pre;

    for(int i=e-1;i>=s;--i){
        if(S[i] == T[0]){
            --pre;
        }
        if(S[i] == T[1]){
            suf_abc += suf_c * pre;
            suf_bc += suf_c;
        }
        if(S[i] == T[2]){
            ++suf_c;
        }
    }

    return;
}

void calcMid(int s, int e, int & mid_a, int & mid_b, int & mid_c, llt & mid_ab, llt & mid_bc, llt & mid_abc){
	mid_a = mid_ab = mid_abc = mid_bc = mid_c = mid_b = 0;
	if(s > e) return;

	int a = T[0] - 'a' + 1;
	int b = T[1] - 'a' + 1;
	int c = T[2] - 'a' + 1;

    mid_a = D[e][0][0][a] - D[s - 1][0][0][a];
	mid_b = D[e][0][0][b] - D[s - 1][0][0][b];
	mid_c = D[e][0][0][c] - D[s - 1][0][0][c];

	mid_ab = D[e][0][a][b] - D[s - 1][0][a][b];
	mid_ab -= mid_b * D[s - 1][0][0][a];

	mid_bc = D[e][0][b][c] - D[s - 1][0][b][c];
	mid_bc -= mid_c * D[s - 1][0][0][b];

	mid_abc = D[e][a][b][c] - D[s - 1][a][b][c];
	mid_abc -= D[s - 1][0][0][a] * mid_bc;
	mid_abc -= D[s - 1][0][a][b] * mid_c;

    return;
}

llt proc(){
    int bs = Block[L], be = Block[R];

    if(bs == be){
        int pre_a, pre_ab, pre_abc;
        if(L == Left[bs] and R == Right[bs]){
			int mid_a, mid_b, mid_c;
			llt mid_ab, mid_bc, mid_abc;
			calcMid(bs, be, mid_a, mid_b, mid_c, mid_ab, mid_bc, mid_abc);		
			return mid_abc;	
		}

        calcPre(L, R, pre_a, pre_ab, pre_abc);
        return pre_abc;
    }

    int pre_a, pre_ab, pre_abc;
    if(L != Left[bs]){
        calcPre(L, Right[bs], pre_a, pre_ab, pre_abc);
    }else{
        pre_a = pre_ab = pre_abc = 0;
    }
    

    int suf_c, suf_bc, suf_abc;
    if(R != Right[be]){
        calcSuf(Left[be], R, suf_c, suf_bc, suf_abc);
    }else{
        suf_c = suf_bc = suf_abc = 0;
    }
    

    int mid_a, mid_b, mid_c;
    llt mid_ab, mid_bc, mid_abc;
    calcMid(bs + (L != Left[bs] ? 1 : 0), be - (R != Right[be] ? 1 : 0), mid_a, mid_b, mid_c, mid_ab, mid_bc, mid_abc);

    llt ans = mid_abc + pre_abc + suf_abc;
    ans += pre_a * (mid_bc + suf_bc);
    ans += pre_ab * (mid_c + suf_c + 0LL);
    ans += (llt)mid_a * suf_bc;
    ans += mid_ab * suf_c;
    ans += (llt)pre_a * mid_b * suf_c;
    return ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> N >> Q >> S;
	S = " " + S;
    init();
    while(Q--){
        cin >> L >> R >> T;
        cout << proc() << "\n";
    }
    return 0;
}